#include "stockremind.h"
#include "ui_stockremind.h"
#include <QMovie>
#include <QTimer>
#include "inquiresql.h"
#include <QKeyEvent>

StockRemind::StockRemind(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::StockRemind)
{
    ui->setupUi(this);
    this->setLayout(ui->MainLayout);
    this->setFixedSize(759,400);
    setWindowTitle(tr("库存不足提醒"));

    //设置自定义的鼠标指针图案
    QCursor *myCursor= new QCursor(QPixmap(":/Mouse/Image/mouse/mouse.png"),15,10);
    setCursor(*myCursor);

    setQtableWidgetHeader();

    QHeaderView* vheaderView = ui->tableWidgetStockRemind->verticalHeader();
    vheaderView->setHidden(true);

    //设置列宽可拖动
//    ui->tableWidgetStockRemind->horizontalHeader()->setResizeMode(QHeaderView::Interactive);
//    ui->tableWidgetStockRemind->verticalHeader()->setResizeMode(QHeaderView::Interactive);

//    动态显示图片
//    QMovie * movie = new QMovie(":/DynamicPicture/Image/DynamicPicture/kiss.gif");
//    ui->labelDynamic->setMovie(movie); //将图片加载到label上
//    movie->start();
//    ui->label->setMovie(movie);

    displayStockRemind();

    //添加时间过滤器
    QWidget::installEventFilter(this);
}

StockRemind::~StockRemind()
{
    delete ui;
}

void StockRemind::displayStockRemind()
{
    m_list.clear();
    m_list = m_sql->InquireCommodityInformation();
    ui->tableWidgetStockRemind->clear();

    setQtableWidgetHeader();

    //判断需要显示的商品信息的行数
    int RowCount = 0;
    for(int i = 0; i < m_list.count(); ++i)
    {
        if (m_list.at(i).get_minStock().toInt()
                >= m_list.at(i).get_stockQuantity().toInt())
        {
            RowCount++;
        }
    }
    ui->tableWidgetStockRemind->setRowCount(RowCount);

    //显示进货商品信息
    RowCount = 0;
    for(int i = 0; i < m_list.count(); ++i)
    {
        if (m_list.at(i).get_minStock().toInt()
                < m_list.at(i).get_stockQuantity().toInt())
        {
            continue;
        }

        QString specifications = m_sql->InquireAttributeValue(Specification,
                                        m_list.at(i).get_specificationsNumber());
        QString category = m_sql->InquireAttributeValue(Category,
                                        m_list.at(i).get_categoryNumber());
        QString color = m_sql->InquireAttributeValue(Color,
                                        m_list.at(i).get_colorNumber());
        QString brand = m_sql->InquireAttributeValue(Brand,
                                        m_list.at(i).get_brandNumbe());
        QString unit = m_sql->InquireAttributeValue(Unit,
                                        m_list.at(i).get_unitNumber());

        QTableWidgetItem *commodityBarcode      = new QTableWidgetItem(m_list.at(i).get_commodityBarcode());
        QTableWidgetItem *commodityName         = new QTableWidgetItem(m_list.at(i).get_commodityName());
        QTableWidgetItem *internalCode          = new QTableWidgetItem(m_list.at(i).get_internalCode());
        QTableWidgetItem *categoryNumber        = new QTableWidgetItem(category);
        QTableWidgetItem *specificationsNumber  = new QTableWidgetItem(specifications);
        QTableWidgetItem *colorNumber           = new QTableWidgetItem(color);
        QTableWidgetItem *brandNumber           = new QTableWidgetItem(brand);
        QTableWidgetItem *unitNumber            = new QTableWidgetItem(unit);
        QTableWidgetItem *stockQuantity         = new QTableWidgetItem(m_list.at(i).get_stockQuantity());
        QTableWidgetItem *minStock              = new QTableWidgetItem(m_list.at(i).get_minStock());

        //设置对齐方式
        commodityBarcode->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        internalCode->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        categoryNumber->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        specificationsNumber->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        colorNumber->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        unitNumber->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        stockQuantity->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        minStock->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );

        //设置文字颜色
        stockQuantity->setTextColor(QColor(255, 0, 0));
        minStock->setTextColor(QColor(255, 0, 255));

        commodityBarcode->setFont(QFont("DejaVu Sans", 11, 2, false));
        commodityName->setFont(QFont("DejaVu Sans", 11, 2, false));
        internalCode->setFont(QFont("DejaVu Sans", 11, 2, false));
        categoryNumber->setFont(QFont("DejaVu Sans", 11, 2, false));
        specificationsNumber->setFont(QFont("DejaVu Sans", 11, 2, false));
        colorNumber->setFont(QFont("DejaVu Sans", 11, 2, false));
        unitNumber->setFont(QFont("DejaVu Sans", 11, 2, false));
        brandNumber->setFont(QFont("DejaVu Sans", 11, 2, false));
        stockQuantity->setFont(QFont("DejaVu Sans", 11, 2, false));
        minStock->setFont(QFont("DejaVu Sans", 11, 2, false));

        ui->tableWidgetStockRemind->setItem(RowCount, 0, commodityBarcode);
        ui->tableWidgetStockRemind->setItem(RowCount, 1, commodityName);
        ui->tableWidgetStockRemind->setItem(RowCount, 2, internalCode);
        ui->tableWidgetStockRemind->setItem(RowCount, 3, categoryNumber);
        ui->tableWidgetStockRemind->setItem(RowCount, 4, specificationsNumber);
        ui->tableWidgetStockRemind->setItem(RowCount, 5, colorNumber);
        ui->tableWidgetStockRemind->setItem(RowCount, 6, unitNumber);
        ui->tableWidgetStockRemind->setItem(RowCount, 7, brandNumber);
        ui->tableWidgetStockRemind->setItem(RowCount, 8, stockQuantity);
        ui->tableWidgetStockRemind->setItem(RowCount, 9, minStock);

        RowCount++;
    }

    ui->tableWidgetStockRemind->setCurrentItem(
                ui->tableWidgetStockRemind->item(RowCount - 1 , 1));

    //设置与文字相匹配的单元格大小
    ui->tableWidgetStockRemind->resizeColumnsToContents();
    ui->tableWidgetStockRemind->resizeRowsToContents();

    setTableWidgetColor();
}

bool StockRemind::eventFilter(QObject *target, QEvent *event)
{
    //键盘事件
    if (event->type() == QEvent::KeyPress)
    {
        QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
        if (keyEvent->key() == Qt::Key_Escape)
        {
            this->hide();
            return true;
        }
    }
    return QWidget::eventFilter(target, event);
}

void StockRemind::setQtableWidgetHeader()
{
    QStringList headers;
    headers << tr("商品编号") << tr("商品名称") << tr("商品内码") << tr("类别")
            << tr("规格") << tr("颜色") << tr("单位")
            << tr("品牌") << tr("库存数量") << tr("报警数量");

    ui->tableWidgetStockRemind->setColumnCount(10);
    ui->tableWidgetStockRemind->setHorizontalHeaderLabels(headers);

    for (int i = 0; i < 10; ++i)
    {
        ui->tableWidgetStockRemind->horizontalHeaderItem(i)->setFont(QFont("Helvetica", 12, 2, false));
        ui->tableWidgetStockRemind->horizontalHeaderItem(i)->setTextColor(QColor(200,111,30));
    }
}

void StockRemind::setTableWidgetColor()
{
    for (int i = 0; i < ui->tableWidgetStockRemind->rowCount(); ++i)
    {
        if (i % 2 == 0)
        {
            for (int j = 0; j < ui->tableWidgetStockRemind->columnCount(); ++j)
            {
                QTableWidgetItem *item = ui->tableWidgetStockRemind->item(i,j);
                if (item)
                {
                    const QColor color = QColor(252,222,156);
                    item->setBackgroundColor(color);
                }
            }
        }
    }
}
